<?php
/**
 * Concrete class for handling Smarty view scripts.
 *
 * @category   AkCom
 * @package    AkCom_View
 * @copyright  Copyright (c) 2006 Rob Allen (http://www.akrabat.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Smarty_View implements Zend_View_Interface
{
    /**
     * @var Smarty
     */
    protected $_engine;

    function __construct($params = null)
    {
        $this->_engine = new Smarty();
        
        // force error_reporting and hope smarty can cope with E_STRICT!
        $this->_engine->error_reporting = E_ALL|E_STRICT;
        
        // I like {{ and }} for delimiters!
        $this->_engine->left_delimiter = '{{';
        $this->_engine->right_delimiter = '}}';
        
        // Render variables XSS safe by default.
        $this->_engine->default_modifiers = array('escape:"htmlall"');

        // AkCom specific: include our default plugins
        $this->_engine->plugins_dir[] = dirname(__FILE__) . '/plugins.';

        // pass any param passed to the constructor to Smarty
        if (is_array($params)) {
            foreach ($params as $key=>$value) {
                $this->_engine->$key = $value;
            }
        }
    }

    public function getEngine()
    {
        return $this->_engine;
    }

    public function setScriptPath($path)
    {
        $this->_engine->template_dir = $path;
    }

    public function __set($key, $val)
    {
        $this->_engine->assign($key, $val);
    }

    public function __get($key)
    {
        return $this->_engine->get_template_vars($key);
    }

    public function __isset($key)
    {
        $var = $this->_engine->get_template_vars($key);
        return !is_null($var); 
    }

    public function __unset($key)
    {
        $this->_engine->clear_assign($key);
    }

    public function assign($spec, $value = null)
    {
        $this->_engine->assign($spec, $value);
    }

    public function clearVars()
    {
        $this->_engine->clear_all_assign();
    }

    public function render($name)
    {
        return $this->_engine->fetch($name);
    }

    public function setPluginsPath($dir)
    {
        $this->_engine->plugins_dir = $dir;
    }

    public function addPluginsDir($dir)
    {
        $this->_engine->plugins_dir[] = $dir;
    }

    public function setCompilePath($dir)
    {
        $this->_engine->compile_dir = $dir;
    }
    
    public function setCachePath($dir)
    {
        $this->_engine->cache_dir = $dir;
    }
    
    public function setCacheLifetime($seconds)
    {
        $this->_engine->cache_lifetime = $seconds;
    }
}

?>